package com.forum.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.bean.Notice;
import com.forum.common.Result;
import com.forum.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper mapper;


    @PostMapping("/save")
    public Result<String> save(@RequestBody Notice n) {
        // 先查一下有没有记录
        List<Notice> notices =  mapper.selectList(new QueryWrapper<>());
        if (!notices.isEmpty()) {
            notices.get(0).setContent(n.getContent());
            mapper.update(notices.get(0),new QueryWrapper<>());
        }else{
            Notice notice = new Notice();
            notice.setContent(n.getContent());
            mapper.insert(notice);
        }
       return Result.ok("保存成功");
    }


    @GetMapping("/get")
    public Result<Notice> get() {
        // 先查一下有没有记录
        List<Notice> notices =  mapper.selectList(new QueryWrapper<>());
        if (!notices.isEmpty()) {
            return Result.ok(notices.get(0));
        }else{
            Notice notice = new Notice();
            notice.setContent("");
            return Result.ok(notice);
        }

    }

}
